package com.yvan.serverless.script;

import com.yvan.serverless.ServerLessProperties;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ScriptService {

    private final ServerLessProperties properties;
    private final ScriptContextHolder holder;

    public ScriptService(ServerLessProperties properties) {
        this.properties = properties;

        ServerLessProperties.TypescriptProperties p = properties.getTypescript();
        this.holder = new ScriptContextHolder(p.getDistLocation(), p.getInspect(), p.getInspectPort());
    }

    /**
     * 执行 js 中的指定方法, 并带入参数
     */
    public Object execute(String path, String methodName, Object... arguments) {
        return holder.getCurrentScriptContext().executeMethod(path, methodName, arguments);
    }

    /**
     * 重新构造 Engine
     */
    public void refresh() {
        holder.rebuildEngin();
    }

    public String createBroker(String url) {
        return holder.getCurrentScriptContext().createBroker(url, properties.getTypescript().getUrl());
    }
}
